﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Text;
using System.Windows.Forms;
using Sunisoft.IrisSkin;

namespace IrisUtilHelpV2
{
    public class IrisToolV2
    {
        public static void HighlightButtonColor<T>(T t, Button button, Color color1, Color color2, Color fontColor)
            where T : Control
        {
            try
            {
                if (t == null)
                    throw new ArgumentNullException("t");
                if (button == null)
                    throw new ArgumentNullException("button");
                foreach (Control ct in t.Controls)
                {
                    if (ct is Button)
                    {
                        Button _button = (Button)ct;
                        if (_button.Name == button.Name)
                            ChangControlColor<Button>(button, color1, color2, fontColor);
                        else
                            RestoreButtonColor(_button);
                    }
                }

            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("HighlightButtonColor Exception:{0}", ex.Message.Trim()));
            }
        }
        const int DisableTag = 9999;
        public static void ChangControlColor<T>(T t, Color color1, Color color2, Color fontColor) where T : Control
        {
            try
            {
                Bitmap _bmp = new Bitmap(t.Width, t.Height);
                using (Graphics _g = Graphics.FromImage(_bmp))
                {
                    Rectangle _r = new Rectangle(0, 0, _bmp.Width, _bmp.Height);
                    using (LinearGradientBrush br = new LinearGradientBrush(
                                                        _r,
                                                        color1,
                                                        color2,
                                                        LinearGradientMode.Vertical))
                    {
                        _g.FillRectangle(br, _r);
                    }
                }
                t.BackgroundImage = _bmp;
                t.ForeColor = fontColor;
                t.Tag = DisableTag;
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("ChangControlColor Exception:{0}", ex.Message.Trim()));
            }
        }
        public static void RestoreButtonColor(Button button)
        {
            try
            {

                button.UseVisualStyleBackColor = true;
                button.Tag = null;
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("RestoreButtonColor Exception:{0}", ex.Message.Trim()));
            }
        }
        public static void SetupTheme(SkinEngine skin, byte[] bytes)
        {
            try
            {
                if (skin == null || bytes == null)
                    throw new ArgumentNullException();
                using (MemoryStream memoryStream = new MemoryStream(bytes))
                    skin.SkinStream = memoryStream;
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("SetupTheme Exception:{0}", ex.Message.Trim()));
            }
        }
    }
}
